package com.xydtech.ecard.statistics.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.statistics.domain.EcardBusiReservationRecord;
import com.xydtech.ecard.statistics.mapper.EcardBusiReservationRecordMapper;
import com.xydtech.ecard.statistics.service.IEcardBusiReservationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预约记录Service业务层处理
 *
 * @author JHON
 * @date 2021-10-09
 */
@Service
public class EcardBusiReservationRecordServiceImpl implements IEcardBusiReservationRecordService {
    @Autowired
    private EcardBusiReservationRecordMapper ecardBusiReservationRecordMapper;

    /**
     * 查询预约记录
     *
     * @param cname 预约记录ID
     * @return 预约记录
     */
    @Override
    public EcardBusiReservationRecord selectEcardBusiReservationRecordById(String cname, String salt) {
        EcardBusiReservationRecord busiReservationRecord = ecardBusiReservationRecordMapper.selectEcardBusiReservationRecordById(cname);
        if (busiReservationRecord != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(busiReservationRecord.getTelNo())) {
                    busiReservationRecord.setTelNo(AESEncryptionUtils.decrypt(busiReservationRecord.getTelNo(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(busiReservationRecord.getCardNum())) {
                    busiReservationRecord.setCardNum(AESEncryptionUtils.decrypt(busiReservationRecord.getCardNum(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return busiReservationRecord;
    }

    /**
     * 查询预约记录列表
     *
     * @param ecardBusiReservationRecord 预约记录
     * @return 预约记录
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "c")
    public List<EcardBusiReservationRecord> selectEcardBusiReservationRecordList(EcardBusiReservationRecord ecardBusiReservationRecord, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReservationRecord.getTelNo())) {
                ecardBusiReservationRecord.setTelNo(AESEncryptionUtils.encrypt(ecardBusiReservationRecord.getTelNo(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardBusiReservationRecord.getCardNum())) {
                ecardBusiReservationRecord.setCardNum(AESEncryptionUtils.encrypt(ecardBusiReservationRecord.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        List<EcardBusiReservationRecord> busiReservationRecordList = ecardBusiReservationRecordMapper.selectEcardBusiReservationRecordList(ecardBusiReservationRecord);
        if (busiReservationRecordList.size() > 0) {
            for (EcardBusiReservationRecord busiReservationRecord : busiReservationRecordList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(busiReservationRecord.getTelNo())) {
                        busiReservationRecord.setTelNo(AESEncryptionUtils.decrypt(busiReservationRecord.getTelNo(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(busiReservationRecord.getCardNum())) {
                        busiReservationRecord.setCardNum(AESEncryptionUtils.decrypt(busiReservationRecord.getCardNum(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return busiReservationRecordList;
    }

}
